<?php
/**
 * 如花拼团开源系统 -- 永久免费
 * =========================================================
 * 官方网址：http://www.phps.shop
 * 作者：光爵【API + 后台】 、 小草【小程序 + WAP】
 * QQ 交流群：728615087
 * Version：1.0.0
 */

namespace app\api\model;

use app\lib\exception\BaseException;
use app\lib\exception\SuccessMessage;
use think\model\concern\SoftDelete;
use think\Db;

class Ticket extends BaseModel
{
    use SoftDelete;
    protected $hidden = ['update_time','create_time'];
    protected $deleteTime = 'delete_time';
    protected $type = [
        'depart_time'  =>  'timestamp:Y-m-d H:i',
        'return_time'  =>  'timestamp:Y-m-d H:i',
    ];

    //机票信息添加修改
    public static function CreateOne($uid,$post)
    {
        $Ticket = new Ticket;
        Db::startTrans();
        try {
            $post['user_id'] = $uid;
            $res  = (empty($post['id'])) ? false : true;
            $Ticket->isUpdate($res)->save($post); //直接通过关联模型来新增
            Db::commit();
        }catch (\Exception $e) {
            Db::rollback();// 回滚事务
            throw new BaseException(['msg'=>'上传签证失败']);
        }
        return json(new SuccessMessage(), 201);
    }

    //获取机票信息
    public static function GetInfo($uid){
        $Ticket = new Ticket;
        $result = $Ticket->where('user_id','=',$uid)->find();
        if($result){
            $time = explode(' ',$result['depart_time']);
            $time1 = explode(' ',$result['return_time']);
            $result['depart_date'] = $time['0'];
            $result['depart_time1'] = $time['1'];
            $result['return_date'] = $time1['0'];
            $result['return_time1'] = $time1['1'];
        }
        return $result;
    }

    //删除机票
    public static function ticket_all_del($uid){
        $res = Ticket::destroy(['user_id'=>$uid]);
        if(!$res){
            throw new BaseException(['删除错误']);
        }else{
            return json(new SuccessMessage(),201);
        }
    }
}